package com.wlcj.www.web;

import java.util.List;

import javax.servlet.http.Cookie;

import com.wlcj.constant.Global;
import com.wlcj.util.RedisUtil;
import com.wlcj.www.model.main.Channel;
import com.wlcj.www.model.main.Content;
import com.wlcj.www.model.main.Penster;
import com.wlcj.www.model.main.Tag;
import com.wlcj.www.util.DataUtil;

public class IndexAction extends BaseAction {
	
	public void index() {
		forwardAction("/data/platform");
	}
	
	public void top() {
		renderJsp("/include/top.jsp");
	}
	
	public void logout() {
		removeSessionAttr(Global.SESSION_LOGIN_KEY);
		removeSessionAttr(Global.SESSION_LOGIN_NAME_KEY);
		Cookie cookie = new Cookie("jr_auth", null); 
		cookie.setMaxAge(0);
        cookie.setPath("/");
        cookie.setDomain(".weilaiwd.com");
        getResponse().addCookie(cookie);
		renderJsp("/include/top.jsp");
	}
	/**
	 * 所有栏目切入口
	 */
	public void column(){
		String channelPath = getPara();
		List<Content> clist = Content.dao.getListByChannelPath(channelPath, 0, 10);
		for (int i = 0; i < clist.size(); i++) {
			Content c=clist.get(i);
			c.set("sort_date", DataUtil.timeTop(String.valueOf(c.get("sort_date"))));
			List<Tag> tagList = Tag.dao.getList(c.getInt("content_id"));
			c.put("tag_list", tagList);
			clist.set(i, c);
		}
		int[] guanggaoweiids = new int[]{2453,2454};
		List<Content> guanggaowei = Content.dao.getListByIds(guanggaoweiids);
		
		Channel channel = Channel.dao.getChannelByPath(channelPath);
		List<Channel> chlist = Channel.dao.getParentChannelById(channel.getInt("channel_id"));
		setAttr("channelPath", channelPath);
		setAttr("chlist", chlist);
		setAttr("chlistSize", chlist.size());
		setAttr("guanggaowei", guanggaowei);
		setAttr("clist", clist);
		renderJsp("/WEB-INF/jsp/newsList/newsList.jsp");
	}
	/**
	 * 文章详细页面切入口
	 */
	public void article(){
		int contentId = getParaToInt();
		Content c= Content.dao.getContentById(contentId);
		int zhuanLanChannelId= 174;
		//如果文章是专栏下的文章那么走以下逻辑
		// 广告位
		int[] guanggaoweiids = new int[]{2452,2453};
		List<Content> guanggaowei = Content.dao.getListByIds(guanggaoweiids);
		
		if(Channel.dao.getChannelByChannelId(c.getInt("channel_id")).getInt("parent_id") == zhuanLanChannelId){
			//专栏文章
				Penster penster = Penster.dao.getPensterById(c.getInt("penster_id")==null?0:c.getInt("penster_id"));
				List<Content> viewsList = Content.dao.getzhuanglanListByViewsAndId(174, 0,8);//专栏下按点击量排行
				List<Penster> zhuanlanList =  Penster.dao.getListByChannelId(174,0, 6);//专栏文章
				List<Content> pensterContentList = Content.dao.getListByPensterId(c.getInt("penster_id"),0,5);//作者其他文章 
				List<Content> xiangguanwenzhangList = Content.dao.getxiangguanwenzhangList(c.getInt("content_id"),0,5);//作者其他文章 根据标签搜索
				List<Channel> chlist = Channel.dao.getParentChannelById(c.getInt("channel_id"));//归属栏目循环
				setAttr("chlist", chlist);
				setAttr("c", c);
				setAttr("penster", penster);
				setAttr("viewsList", viewsList);//专栏下按点击量排行
				setAttr("zhuanlanList", zhuanlanList);//专栏文章
				setAttr("pensterContentList", pensterContentList);//作者其他文章 
				setAttr("xiangguanwenzhangList", xiangguanwenzhangList);//作者其他文章 根据标签搜索
				renderJsp("/WEB-INF/jsp/news/zhuanlancontent.jsp");
		}else{
			//普通文章
			List<Content> xiangguanwenzhangList = Content.dao.getxiangguanwenzhangList(c.getInt("content_id"),0,10);//作者其他文章 根据标签搜索
			List<Content> viewsList = Content.dao.getViewList(0,8);//热门文章、按点击量排行
			List<Channel> chlist = Channel.dao.getParentChannelById(c.getInt("channel_id"));
			setAttr("xiangguanwenzhangList", xiangguanwenzhangList);
			setAttr("viewsList", viewsList);
			setAttr("chlist", chlist);
			setAttr("c", c);
			renderJsp("/WEB-INF/jsp/news/news-show.jsp");
			//普通文章
		}
		setAttr("guanggaowei", guanggaowei);
	}
	public void getmorenewslist(){
		String channelPath = getPara("channelPath");
		int start = getParaToInt("start")*10;
		List<Content> clist = Content.dao.getListByChannelPath(channelPath, start, 10);
		for (int i = 0; i < clist.size(); i++) {
			Content c=clist.get(i);
			c.set("sort_date", DataUtil.timeTop(String.valueOf(c.get("sort_date"))));
			List<Tag> tagList = Tag.dao.getList(c.getInt("content_id"));
			c.put("tag_list", tagList);
			clist.set(i, c);
		}
		setAttr("clist", clist);
		renderJsp("/WEB-INF/jsp/newsList/newsmore.jsp");
	}
}
